package com.ocom.accesservice.controller;


import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ocom.accesservice.service.FileDownloadInfoService;
import com.ocom.accesservice.vo.filedownloadinfo.GetDownLoadListVo;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.master.EntityFileDownloadInfo;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.resp.Result;
import com.ocom.common.utils.ResultUtil;
import com.ocom.common.vo.SearchMachListVo;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
public class FileDownloadInfoController {
    private final Logger log = LoggerFactory.getLogger(DeviceListController.class);

    @Autowired
    private FileDownloadInfoService fileDownloadInfoService;

    /**
     * 更改文件状态 （/updateFileStatus)
     */
    @PostMapping(AccessServiceUrl.URL_UpdateFileStatus)
    public Result UpdateFileStatus(@Validated @RequestBody EntityFileDownloadInfo entityFileDownloadInfo){
        log.info("更改文件状态请求参数{{}}",entityFileDownloadInfo);
        try {
            fileDownloadInfoService.UpdateFileStatus(entityFileDownloadInfo);
        }catch (Exception exception){
            log.error("更改文件状态失败:{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"更改文件状态失败");
        }

        return ResultUtil.success();
    }


    /**
     * 添加文件下载申请 （/sendExcelApply）
     * */
    @PostMapping(AccessServiceUrl.URL_SendExcelApply)
    public Result SendExcelApply(@Validated @RequestBody EntityFileDownloadInfo entityFileDownloadInfo){
        log.info("添加文件下载申请请求参数{{}}",entityFileDownloadInfo);
        Long id = null;
        try {
            id = fileDownloadInfoService.SendExcelApply(entityFileDownloadInfo);

        }catch (Exception exception){
            log.error("添加文件下载失败:{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"添加文件下载失败");
        }

        return ResultUtil.success(id);
    }

    /**
     * 获取列表 （/getDownLoadList）
     * */
    @PostMapping(AccessServiceUrl.URL_GetDownLoadList)
    public Result GetDownLoadList(@Validated @RequestBody GetDownLoadListVo getDownLoadListVo){
        log.info("获取列表请求参数{{}}",getDownLoadListVo);
        List<EntityFileDownloadInfo> entityFileDownloadInfos = null;
        Map<String, Object> map = new HashMap<>();
        try {
            PageHelper.startPage(getDownLoadListVo.getPage(),getDownLoadListVo.getSize());
            entityFileDownloadInfos = fileDownloadInfoService.GetDownLoadList(getDownLoadListVo.getPId());
            PageInfo<EntityFileDownloadInfo> entityFileDownloadInfoPageInfo = new PageInfo<>(entityFileDownloadInfos);
            map.put("list",entityFileDownloadInfos);
            map.put("total",entityFileDownloadInfoPageInfo.getTotal());
        }catch (Exception exception){
            log.error("获取列表失败:{{}}",exception.getMessage());
            return ResultUtil.error(ResultEnum.Err_2009,"获取列表失败");
        }

        return ResultUtil.success(map);
    }

}
